<?php

namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class Power extends Model
{

	// /**
	//  * [showListModel 添加时上级名称显示model]
	//  * @Author   孙丽男
	//  * @DateTime 2018-06-05
	//  * @version  6.2
	//  * @return   [json]    [$result]
	//  */
	// public function showParentModel(){

	// 	$powerObj = Db::table('power')
	// 					->select(['id','name as value'])
	// 					->get();
	// 	$powerArr = json_decode(json_encode($powerObj),true);

	// 	if ($powerArr) {
			
	// 		$comm = new Common;
	// 		//处理数组
	// 		$data = $comm->createTree($powerArr);
	// 		$result['code'] = 1000;
 //    		$result['msg'] = '查询成功';
 //    		$result['data'] = $data;
 //    		return $result;
	// 	}else{

	// 		$result['code'] = 1004;
 //    		$result['msg'] = '查询部门失败';
 //    		return $result;
	// 	}

	// 	return json_encode($result);
	// }
	/**
	 * [addRoleModel 添加权限model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [权限信息]  array  $powerArr
	 * @version  6.2
	 */
	public function addPowerModel($powerArr){

		$result = array();
		$powerArr['update_user'] = session('username');
		$powerArr['update_time'] = date('Y-m-d H:i:s');

		$checkData = $this->checkData($powerArr);

		if ($checkData) {
			
			return $checkData;
		}
		$levelObj = Db::table('power')
					->where('id',$powerArr['pid'])
					->select('level')
					->first();
		$levelArr = json_decode(json_encode($levelObj),true);
		$level = $levelArr['level'] + 1;
		$powerArr['level'] = $level;
		$power = Db::table('power')
					->insert($powerArr);
		if ($power) {
				
			$result['code'] = 1000;
			$result['msg'] = '添加成功';

		}else{

			$result['code'] = 1004;
			$result['msg'] = '添加失败';
		}

		return json_encode($result);
	}

	/**
	 * [findPowerModel 权限列表model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [查询条件]  array  $where
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function findPowerModel($where){

		$result = array();
		$powerObj = Db::table('power')
						->where($where)
						->orderBy('update_time', 'desc')
						->get();
		$powerArr = json_decode(json_encode($powerObj),true);

		if ($powerArr) {

			//整理数组
			foreach ($powerArr as $key => $value) {
				
				if ($value['pid'] != 0) {

					$pname = Db::table('power')
					->where('id',$powerArr[$key]['pid'])
					->select('number')
					->first();

					$pnameArr = json_decode(json_encode($pname), true);

					$powerArr[$key]['pnumber'] = $pnameArr['number'];
					
				}else{

					$powerArr[$key]['pnumber'] = '';
				}

				if ($value['status'] == 1) {
					$powerArr[$key]['status'] = '启用';
				}else{
					$powerArr[$key]['status'] = '冻结';
				}
			}
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $powerArr;		

		}else{
			$result['code'] = 1004;
    		$result['msg'] = '查询失败';
		}
		return json_encode($result);
	}

	/**
	 * [deletePowerModel 删除权限model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [角色id数组] array $powerIdArr
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function deletePowerModel($powerIdArr){


		//删除权限
		$deletePower = Db::table('power')
						->whereIn('id', $powerIdArr)
						->delete();
		if ($deletePower) {
			//删除角色权限
			$deleteRolePower = Db::table('role_power')
							->whereIn('power_id', $powerIdArr)
							->delete();	

			$result['code'] = 1000;
			$result['msg'] = '删除成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '删除失败';
		}
		
		return json_encode($result);
	}

	/**
	 * [updatePowerModel 修改权限信息model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-05
	 * @param    [权限id] int $powerId
	 * @param    [需要修改的信息] array $updateInfo
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function updatePowerModel($powerId, $updateInfo){

		$checkData = $this->checkData2($updateInfo ,$powerId);
		
		if ($checkData) {
			
			return $checkData;
		}
		$updateInfo['update_user'] = session('username');
		$updateInfo['update_time'] = date('Y-m-d H:i:s');
		$levelObj = Db::table('power')
					->where('id',$updateInfo['pid'])
					->select('level')
					->first();
		$levelArr = json_decode(json_encode($levelObj),true);
		$level = $levelArr['level'] + 1;
		$updateInfo['level'] = $level;
		$updatePower = Db::table('power')
						->where('id', $powerId)
						->update($updateInfo);
		if ($updatePower) {

			$result['code'] = 1000;
			$result['msg'] = '修改成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '修改失败';
		}
		
		return json_encode($result);
	}

	/**
	 * [updateStatusModel 修改权限状态model]
	 * @Author   孙丽男
	 * @DateTime 2018-06-04
	 * @param    [权限id组] array $powerIdArr
	 * @param    [状态] int $status
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function updateStatusModel($powerIdArr, $status){

		$update['status'] = $status;
		$update['update_user'] = session('username');
		$update['update_time'] = date('Y-m-d H:i:s');
		$updateStatus = Db::table('power')
						->whereIn('id', $powerIdArr)
						->update($update);
		if ($updateStatus) {

			$result['code'] = 1000;
			$result['msg'] = '修改成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '修改失败';
		}
		
		return json_encode($result);

	}

	/**
	 * [checkData 查询数据信息是否存在]
	 * @Author   孙丽男
	 * @DateTime 2018-06-06
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function checkData($powerArr){

		$powerName  = Db::table('power')
					->where(['pid'=>$powerArr['pid'], 'name'=> $powerArr['name']])
					->first();
		if ($powerName) {

			$result['code'] = 1005;
			$result['msg'] = '父级下已存在该菜单名称';
			return json_encode($result);

		}

		$powerSort = Db::table('power')
					->where(['pid'=>$powerArr['pid'], 'sort'=> $powerArr['sort']])
					->first();
		if ($powerSort) {

			$result['code'] = 1005;
			$result['msg'] = '父级下已存在该排序';
			return json_encode($result);

		}	

		$poweUrl = Db::table('power')
					->where(['url'=>$powerArr['url']])
					->first();
		if ($poweUrl) {

			$result['code'] = 1005;
			$result['msg'] = '地址已存在';
			return json_encode($result);

		}

		return false;
	}

	/**
	 * [checkData 查询数据信息是否存在]
	 * @Author   孙丽男
	 * @DateTime 2018-06-06
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function checkData2($powerArr,$pawerId){

		$powerName  = Db::table('power')
					->where(['pid'=>$powerArr['pid'], 'name'=> $powerArr['name']])
					->where('id','<>',$pawerId)
					->first();
		if ($powerName) {

			$result['code'] = 1005;
			$result['msg'] = '父级下已存在该菜单名称';
			return json_encode($result);

		}

		$powerSort = Db::table('power')
					->where(['pid'=>$powerArr['pid'], 'sort'=> $powerArr['sort']])
					->where('id','<>',$pawerId)
					->first();
		if ($powerSort) {

			$result['code'] = 1005;
			$result['msg'] = '父级下已存在该排序';
			return json_encode($result);

		}	

		$poweUrl = Db::table('power')
					->where(['url'=>$powerArr['url']])
					->where('id','<>',$pawerId)
					->first();
		if ($poweUrl) {

			$result['code'] = 1005;
			$result['msg'] = '地址已存在';
			return json_encode($result);

		}

		return false;
	}
}